package com.hua.yuan.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hua.yuan.R;
import com.hua.yuan.base.Constents;
import com.hua.yuan.ui.ActivityTeJia;
import com.hua.yuan.ui.VideoDisActivity;
import com.hua.yuan.ui.data.VideoDataRequest;
import com.hua.yuan.ui.module.MovieInfo69Bean;
import com.hua.yuan.util.CheckLoginUtil;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cm.hua.yuan.common.utils.PreferenceUtils;
import okhttp3.Call;


public class VideoItemViewFor extends RelativeLayout {

    private Context context;

    private LinearLayout videoMoreTipLl;//点击更多

    private ImageView videoImage1;
    private TextView videoItemName1;
    private TextView videoDb1;
    private TextView videoDai1;
    private TextView videoPlayCount1;


    private ImageView videoImage2;
    private TextView videoItemName2;
    private TextView videoDb2;
    private TextView videoDai2;
    private TextView videoPlayCount2;


    private ImageView videoImage3;
    private TextView videoItemName3;
    private TextView videoDb3;
    private TextView videoDai3;
    private TextView videoPlayCount3;


    private ImageView videoImage4;
    private TextView videoItemName4;
    private TextView videoDb4;
    private TextView videoDai4;
    private TextView videoPlayCount4;


    private LinearLayout video_fresh_ll;
    private ImageView video_fresh_image;

    private boolean flag = true;

    private List<MovieInfo69Bean> list;

    private void init(Context context) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.video_two_tms_for_view, this);
        initView();
        initData();


    }


    public VideoItemViewFor(Context context) {
        super(context);
        init(context);
    }

    public VideoItemViewFor(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public VideoItemViewFor(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    @SuppressLint("NewApi")
    public VideoItemViewFor(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    public void setData(List<MovieInfo69Bean> list) {

        this.list = list;

        if (list != null && list.size() >= 4) {
            MovieInfo69Bean bean1 = list.get(0);
            showUi(videoImage1, videoItemName1, videoDb1, videoDai1, videoPlayCount1, bean1);

            MovieInfo69Bean bean2 = list.get(1);
            showUi(videoImage2, videoItemName2, videoDb2, videoDai2, videoPlayCount2, bean2);

            MovieInfo69Bean bean3 = list.get(2);
            showUi(videoImage3, videoItemName3, videoDb3, videoDai3, videoPlayCount3, bean3);

            MovieInfo69Bean bean4 = list.get(3);
            showUi(videoImage4, videoItemName4, videoDb4, videoDai4, videoPlayCount4, bean4);
        }

    }


    private void initView() {

        videoMoreTipLl = findViewById(R.id.videoMoreTipLl);

        videoImage1 = findViewById(R.id.videoImage1);
        videoItemName1 = findViewById(R.id.videoItemName1);
        videoDb1 = findViewById(R.id.videoDb1);
        videoDai1 = findViewById(R.id.videoDai1);
        videoPlayCount1 = findViewById(R.id.videoPlayCount1);

        videoImage2 = findViewById(R.id.videoImage2);
        videoItemName2 = findViewById(R.id.videoItemName2);
        videoDb2 = findViewById(R.id.videoDb2);
        videoDai2 = findViewById(R.id.videoDai2);
        videoPlayCount2 = findViewById(R.id.videoPlayCount2);


        videoImage3 = findViewById(R.id.videoImage3);
        videoItemName3 = findViewById(R.id.videoItemName3);
        videoDb3 = findViewById(R.id.videoDb3);
        videoDai3 = findViewById(R.id.videoDai3);
        videoPlayCount3 = findViewById(R.id.videoPlayCount3);


        videoImage4 = findViewById(R.id.videoImage4);
        videoItemName4 = findViewById(R.id.videoItemName4);
        videoDb4 = findViewById(R.id.videoDb4);
        videoDai4 = findViewById(R.id.videoDai4);
        videoPlayCount4 = findViewById(R.id.videoPlayCount4);


        video_fresh_ll = findViewById(R.id.video_fresh_ll);
        video_fresh_image = findViewById(R.id.video_fresh_image);


    }

    private void initData() {

        videoMoreTipLl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, ActivityTeJia.class));

            }
        });

        video_fresh_ll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                video_fresh_image.startAnimation(AnimationUtils.loadAnimation(context, R.anim.rotate_refresh));

                freshData();

            }
        });


        videoDai1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        videoDai2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        videoDai3.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        videoDai4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

        videoImage1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                if (list != null && list.size() > 0) {

                    if (CheckLoginUtil.check()) {
                        String id = list.get(0).getId();
                        context.startActivity(new Intent(context, VideoDisActivity.class).putExtra("id", id));

                    } else {
                        CheckLoginUtil.goToLogin((Activity) context);
                    }

                }

            }
        });

        videoImage2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list != null && list.size() > 1) {

                    if (CheckLoginUtil.check()) {
                        String id = list.get(1).getId();
                        context.startActivity(new Intent(context, VideoDisActivity.class).putExtra("id", id));

                    } else {
                        CheckLoginUtil.goToLogin((Activity) context);
                    }

                }
            }
        });

        videoImage3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list != null && list.size() > 2) {

                    if (CheckLoginUtil.check()) {
                        String id = list.get(2).getId();
                        context.startActivity(new Intent(context, VideoDisActivity.class).putExtra("id", id));

                    } else {
                        CheckLoginUtil.goToLogin((Activity) context);
                    }

                }
            }
        });

        videoImage4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list != null && list.size() > 3) {

                    if (CheckLoginUtil.check()) {
                        String id = list.get(3).getId();
                        context.startActivity(new Intent(context, VideoDisActivity.class).putExtra("id", id));

                    } else {
                        CheckLoginUtil.goToLogin((Activity) context);
                    }

                }
            }
        });


    }


    @SuppressLint("SetTextI18n")
    private void showUi(ImageView imageView,
                        TextView tv1, TextView tv2,
                        TextView tv3, TextView tv4, MovieInfo69Bean bean) {

        if (bean == null) {
            return;
        }


        if (imageView == null) {
            return;
        }

        if (tv1 == null) {
            return;
        }
        if (tv2 == null) {
            return;
        }
        if (tv3 == null) {
            return;
        }
        if (tv4 == null) {
            return;
        }

        //显示图片
        Glide.with(context)
                .load(bean.getImg_url())
                .into(imageView);
        //显示名字
        tv1.setText(bean.getTitle());

        //显示代币1
        tv2.setText(bean.getNow_price() + "代币");

        //显示代币2
        tv3.setText(bean.getOrigin_price() + "代币");

        //显示播放数
        Integer num;
        try {
            num = Integer.parseInt(bean.getPlay_nums());
        } catch (NumberFormatException e) {
            num = 0;
        }
        if (num > 999) {
            tv4.setText("播放数：999+");
        } else {
            tv4.setText("播放数：" + num);
        }
    }


    /**
     * 换一批数据
     */
    private void freshData() {

        if (flag) {

            flag = false;
            String token = PreferenceUtils.getString(Constents.TOKEN, "");

            final int from = PreferenceUtils.getInt(Constents.TEI_JIA_DATA_START, 0);

            VideoDataRequest.getMovies(token, 4, from, 4, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {

                    flag = true;


                }

                @Override
                public void onResponse(String response, int id) {

                    flag = true;

                    try {
                        JSONObject jsonObject = new JSONObject(response);

                        if (!jsonObject.isNull("errno")) {
                            String code = jsonObject.getString("errno");

                            if ("0".equals(code)) {

                                if (!jsonObject.isNull("data")) {


                                    JSONArray dataJson = jsonObject.getJSONArray("data");

                                    Gson gson = new Gson();

                                    List<MovieInfo69Bean> list = new ArrayList<>();

                                    for (int i = 0; i < dataJson.length(); i++) {
                                        JSONObject data = dataJson.getJSONObject(i);
                                        MovieInfo69Bean bean = gson.fromJson(data.toString(), MovieInfo69Bean.class);
                                        list.add(bean);
                                    }


                                    if (list.size() >= 4) {
                                        PreferenceUtils.setInt(Constents.TEI_JIA_DATA_START, from + 4);
                                        setData(list);
                                    }

                                    if (list.size() == 0) {
                                        PreferenceUtils.setInt(Constents.TEI_JIA_DATA_START, 0);

                                    }


                                }


                            }

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            });


        }


    }


}

